package cn.csc.module.order.mq.producer;

import cn.csc.framework.mq.core.bus.AbstractBusProducer;
import cn.csc.module.order.message.OrderCancelMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageConst;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;

/**
 * 订单超时未支付自动取消消息发送
 * topic: order_cancel
 * 延迟消息
 * @author liuchuang
 * @version 1.0
 * @date 2024/1/29 17:17
 * @modify
 */
@Slf4j
@Component
public class OrderCancelProducer extends AbstractBusProducer {

    @Resource
    private StreamBridge streamBridge;

    // 延迟消息 1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h (1-18)
    //  1级为1s，依次类推

    /**
     * 发送 {@link OrderCancelMessage} 消息
     *
     * @param orderIds 订单id
     */
    public Boolean sendOrderCancelMessage(List<BigInteger> orderIds) {
        OrderCancelMessage cancelMessage = new OrderCancelMessage(orderIds);

        Message<OrderCancelMessage> message = MessageBuilder.withPayload(cancelMessage)
                // 延迟消息等级
                .setHeader(MessageConst.PROPERTY_DELAY_TIME_LEVEL, 16)  // 延迟30min
                .build();
        return streamBridge.send("orderCancel-out-0", message);
    }

}
